layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'form'], function(){
    var $ = layui.$, form = layui.form;

    var id = $('#id').val();
    var type = $("input[name='type']:checked").val();
    var data = {'id': id};
    var channel;
    post('channel/toEdit', data, false,function (data) {
        channel = data.channel;
        if(channel.type == null){
            channel.type = type;
        }
        if(channel.type == 1){  //主播渠道
            $('input[name="type"]').eq(0).attr('checked', 'checked');
            $('.type2').hide();
            $('.type2 input').removeAttr('lay-verify');
            $('.type3').hide();
            $('.type3 input').removeAttr('lay-verify');
            $('.type1').show();
            $('.type1 input').attr('lay-verify', 'required');
        }else if(channel.type == 2){    //cps渠道
            $('input[name="type"]').eq(1).attr('checked', 'checked');
            $('.type1').hide();
            $('.type1 input').removeAttr('lay-verify');
            $('.type3').hide();
            $('.type3 input').removeAttr('lay-verify');
            $('.type2').show();
            $('.type2 input').attr('lay-verify', 'required');
        }else if(channel.type == 3){
            $('input[name="type"]').eq(2).attr('checked', 'checked');
            $('.type1').hide();
            $('.type1 input').removeAttr('lay-verify');
            $('.type2').hide();
            $('.type2 input').removeAttr('lay-verify');
            $('.type3').show();
            $('.type3 input').attr('lay-verify', 'required');
        }

        if(id == ''){
            if(channel.type == 1){
                $('#first_invite_recharge_rate').val(channel.host_channel_first_invite_recharge_rate);
                $('#first_invite_rate').val(channel.host_channel_first_invite_rate);
                $('#second_invite_recharge_rate').val(channel.host_channel_second_invite_recharge_rate);
                $('#second_invite_rate').val(channel.host_channel_second_invite_rate);
                $('#first_invite_vip_rate').val(channel.host_channel_first_invite_vip_rate);
                $('#first_invite_user_rate').val(channel.host_channel_first_invite_user_rate);
            }else if(channel.type == 2){
                $('#first_invite_recharge_rate').val(channel.cps_channel_first_invite_recharge_rate);
                $('#first_invite_rate').val(channel.cps_channel_first_invite_rate);
                $('#second_invite_rate').val(channel.cps_channel_second_invite_rate);
                $('#first_invite_vip_rate').val(channel.cps_channel_first_invite_vip_rate);
            }else if(channel.type == 3){
                $('#first_invite_recharge_rate').val(channel.union_channel_first_invite_recharge_rate);
                $('#first_invite_rate').val(channel.union_channel_first_invite_rate);
                $('#second_invite_rate').val(channel.union_channel_second_invite_rate);
                $('#first_invite_vip_rate').val(channel.union_channel_first_invite_vip_rate);
                $('#first_invite_user_rate').val(channel.union_channel_first_invite_user_rate);
            }
        }else{
            $('input[name="type"]').attr('disabled', 'disabled');
            $('#userId').attr('disabled', 'disabled');
            $('#password').removeAttr('lay-verify');
            $('#password').parent().parent().hide();

            if(channel.type == 1){
                $('#first_invite_recharge_rate').val(channel.first_invite_recharge_rate);
                $('#first_invite_rate').val(channel.first_invite_rate);
                $('#second_invite_recharge_rate').val(channel.second_invite_recharge_rate);
                $('#second_invite_rate').val(channel.second_invite_rate);
                $('#first_invite_vip_rate').val(channel.first_invite_vip_rate);
                $('#first_invite_user_rate').val(channel.first_invite_user_rate);
            }else if(channel.type == 2){
                $('#first_invite_recharge_rate').val(channel.first_invite_recharge_rate);
                $('#first_invite_rate').val(channel.first_invite_rate);
                $('#second_invite_rate').val(channel.second_invite_rate);
                $('#first_invite_vip_rate').val(channel.first_invite_vip_rate);
            }else if(channel.type == 3){
                $('#first_invite_recharge_rate').val(channel.first_invite_recharge_rate);
                $('#first_invite_rate').val(channel.first_invite_rate);
                $('#second_invite_rate').val(channel.second_invite_rate);
                $('#first_invite_vip_rate').val(channel.first_invite_vip_rate);
                $('#first_invite_user_rate').val(channel.first_invite_user_rate);
            }
        }

        form.render('radio');

        $('#username').val(channel.username);
        $('#userId').val(channel.userId);
        $('#realname').val(channel.realname);
        $('#idNo').val(channel.idNo);
        $('#linkMail').val(channel.linkMail);
        $('#linkQq').val(channel.linkQq);
        $('#linkWx').val(channel.linkWx);
        $('#linkPhone').val(channel.linkPhone);
        $('#linkAlipayName').val(channel.linkAlipayName);
        $('#linkAlipayNo').val(channel.linkAlipayNo);

    });

    /**
     * 渠道类型选择
     */
    form.on('radio(type)', function(data){
        var value = data.value;
        if(value == 1){
            $('.type2').hide();
            $('.type2 input').removeAttr('lay-verify');
            $('.type3').hide();
            $('.type3 input').removeAttr('lay-verify');
            $('.type1').show();
            $('.type1 input').attr('lay-verify', 'required');

            $('#first_invite_recharge_rate').val(channel.host_channel_first_invite_recharge_rate);
            $('#first_invite_rate').val(channel.host_channel_first_invite_rate);
            $('#second_invite_recharge_rate').val(channel.host_channel_second_invite_recharge_rate);
            $('#second_invite_rate').val(channel.host_channel_second_invite_rate);
            $('#first_invite_vip_rate').val(channel.host_channel_first_invite_vip_rate);
            $('#first_invite_user_rate').val(channel.host_channel_first_invite_user_rate);
        }else if(value == 2){
            $('input[name="type"]').eq(1).attr('checked', 'checked');
            $('.type1').hide();
            $('.type1 input').removeAttr('lay-verify');
            $('.type3').hide();
            $('.type3 input').removeAttr('lay-verify');
            $('.type2').show();
            $('.type2 input').attr('lay-verify', 'required');

            $('#first_invite_recharge_rate').val(channel.cps_channel_first_invite_recharge_rate);
            $('#first_invite_rate').val(channel.cps_channel_first_invite_rate);
            $('#second_invite_rate').val(channel.cps_channel_second_invite_rate);
            $('#first_invite_vip_rate').val(channel.cps_channel_first_invite_vip_rate);
        }else if(value == 3){
            $('input[name="type"]').eq(2).attr('checked', 'checked');
            $('.type1').hide();
            $('.type1 input').removeAttr('lay-verify');
            $('.type2').hide();
            $('.type2 input').removeAttr('lay-verify');
            $('.type3').show();
            $('.type3 input').attr('lay-verify', 'required');

            $('#first_invite_recharge_rate').val(channel.union_channel_first_invite_recharge_rate);
            $('#first_invite_rate').val(channel.union_channel_first_invite_rate);
            $('#second_invite_rate').val(channel.union_channel_second_invite_rate);
            $('#first_invite_vip_rate').val(channel.union_channel_first_invite_vip_rate);
            $('#first_invite_user_rate').val(channel.union_channel_first_invite_user_rate);
        }
    });
});